package com.gsoe.problem.controller;

import com.gsoe.base.exception.ProblemException;
import com.gsoe.base.model.Result;
import com.gsoe.problem.dto.SubmitDTO;
import com.gsoe.problem.dto.TestDTO;
import com.gsoe.problem.service.TestProblemService;
import com.gsoe.problem.service.TestService;
import com.gsoe.problem.vo.SubmitVO;
import com.gsoe.problem.vo.TestProblemVO;
import com.gsoe.problem.vo.TestVO;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 测验表 前端控制器
 * </p>
 *
 * @author xiaoma
 * @since 2024-10-27
 */
@RestController
@RequestMapping("/problem/test")
public class TestController {

    @Autowired
    private TestService testService;

    @Autowired
    private TestProblemService testProblemService;


    @PostMapping("/createTest")
    public Result<Long> createTest(@RequestBody @Valid TestDTO testDTO) {
        return Result.success(testService.createTest(testDTO));
    }

    @GetMapping("/getProblemListTest/{id}")
    public Result<TestProblemVO> getTestProblemList(@PathVariable("id") Long id) {
        if(id==null)throw new ProblemException("测验不存在");
        return Result.success(testProblemService.getTestProblemList(id));
    }

    @GetMapping("/getTextList")
    public Result<List<TestVO>> getTextList(@RequestParam(required = false) String textName,@RequestParam(required = false) Long textId,@RequestParam(required = false) Integer status,@RequestParam(required = false) Long userId) {
        return Result.success(testService.getTextList(textName,textId,status,userId));
    }

    @GetMapping("/getTextRemainingTime/{textId}")
    public Result<Long> getTextRemainingTime(@PathVariable(value = "textId") Long textId) {
        return Result.success(testService.getTextRemainingTime(textId));
    }
    @PostMapping("/submit")
    public Result<SubmitVO> submit(@RequestBody SubmitDTO dto) {
        return Result.success(testService.submit(dto));
    }

    @GetMapping("/reSubmit/{textId}")
    public Result<String> reSubmit(@PathVariable Long textId){
        testService.reSubmit(textId);
        return Result.success("ok");
    }

}
